package com.sw.small_world.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sw.small_world.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我界面中自定的组合控件
 * Created by zs on 2015/11/6.
 */
public class MeItemRelativeLayout extends RelativeLayout {
    @Bind(R.id.iv_image)
    ImageView iv_image;
    @Bind(R.id.tv_des)
    TextView tv_des;
    @Bind(R.id.iv_arrow)
    ImageView ivArrow;

    public MeItemRelativeLayout(Context context) {
        super(context);
        initView(context);
    }

    public MeItemRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        getAttrs(context,attrs);

    }

    public MeItemRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        getAttrs(context, attrs);
    }

    /**
     * 获取自定义属性并设置内容
     */
    private void getAttrs(Context context,AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ItemSettingsView);
        // 获取文本内容
        String text = a.getString(R.styleable.ItemSettingsView_text_des);
        // 获取图片的资源id
        int resId = a.getResourceId(R.styleable.ItemSettingsView_background_img,0);
        // 设置文本内容和图片
        tv_des.setText(text);
        iv_image.setImageResource(resId);

    }

    /**
     * 初始化组合控件的布局文件
     */
    private void initView(Context context) {
        View view = View.inflate(context, R.layout.widgets_meitem_rl, this);
        ButterKnife.bind(this,view);
    }
}
